﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Engine.Helpers.*;
    import Classes.util.*;
    import __AS3__.vec.*;
    import Classes.effects.*;
    import Modules.workers.*;
    import Display.*;
    import Display.MarketUI.*;
    import GameMode.*;
    import Modules.factory.ui.*;
    import com.zynga.skelly.util.*;
    import Modules.workers.transactions.*;

    public class Factory extends Plot {

        protected var m_updateUI:Function;

        public function Factory(_arg1:String, _arg2:int=0){
            super(_arg1, _arg2);
            m_isRoadVerifiable = true;
        }
        public static function isFactoryInPlayerWorld():Boolean{
            var _local1:Array = Global.world.getObjectsByClass(Factory);
            if ((((_local1.length > 0)) && (!(Global.isVisiting())))){
                return (true);
            };
            return (false);
        }

        override protected function showCatalogWindow():Catalog{
            return (UI.displayCatalog("factory_contract", null, true, "factory_contract", true, false, true));
        }
        override protected function getCurrentImage():ItemImageInstance{
            return (getPlotImage());
        }
        override public function isPlantable(_arg1:String):Boolean{
            var _local2:Item = Global.gameSettings.getItemByName(_arg1);
            return ((_local2.type == "factory_contract"));
        }
        override protected function getPlowedCursor():Class{
            if (isNeedingRoad){
                return (EmbeddedArt.hud_act_move);
            };
            return (null);
        }
        override protected function getGrownCursor():Class{
            if (isNeedingRoad){
                return (EmbeddedArt.hud_act_move);
            };
            return (null);
        }
        override public function plant(_arg1:String):Boolean{
            var _local3:FactoryWorkers;
            if (isNeedingRoad){
                return (false);
            };
            var _local2:Boolean = super.plant(_arg1);
            if (_local2){
                Global.factoryWorkerManager.clearWorkers(getWorkerBucket());
                _local3 = (Global.factoryWorkerManager.addAndGetWorkers(getWorkerBucket()) as FactoryWorkers);
                _local3.contractName = harvestingDefinition.name;
                this.showWorkerDialog();
            };
            return (_local2);
        }
        public function canPurchaseWorker():Boolean{
            var _local1:FactoryWorkers = (Global.factoryWorkerManager.addAndGetWorkers(getWorkerBucket()) as FactoryWorkers);
            return (((((_local1.canAddWorker()) && (harvestingDefinition.workers))) && (Global.player.canBuyCash(harvestingDefinition.workers.cashCost, true))));
        }
        public function purchaseWorker():void{
            var _local1:FactoryWorkers = (Global.factoryWorkerManager.addAndGetWorkers(getWorkerBucket()) as FactoryWorkers);
            _local1.addWorker(String(-((_local1.numPurchasedWorkers + 1))));
            _local1.numPurchasedWorkers++;
            Global.player.cash = (Global.player.cash - harvestingDefinition.workers.cashCost);
            GameTransactionManager.addTransaction(new TWorkerPurchase("factories", getWorkerBucket()));
        }
        override protected function onStateChanged(_arg1:String, _arg2:String):void{
            if ((((_arg1 == STATE_PLANTED)) && ((_arg2 == STATE_GROWN)))){
                GameTransactionManager.addTransaction(new TWorkersSync("factories", Global.factoryWorkerManager));
            };
            super.onStateChanged(_arg1, _arg2);
        }
        override public function harvest():Boolean{
            var _local1:Boolean = super.harvest();
            Global.factoryWorkerManager.clearWorkers(getWorkerBucket());
            return (_local1);
        }
        override protected function processPlantedState():void{
            this.showWorkerDialog();
        }
        private function showWorkerDialog():void{
            var dlg:* = null;
            dlg = new FactoryGrowStateDialog(this, "FactoryWorkerDialog", "", null, null, null);
            UI.displayPopup(dlg);
            this.m_updateUI = Curry.curry(function ():void{
                (dlg as FactoryGrowStateDialog).refresh();
            }, dlg);
        }
        override public function isVisitorInteractable():Boolean{
            return (false);
        }
        override public function get isHighlightable():Boolean{
            var _local1:Boolean;
            var _local2:GameMode = Global.world.getTopGameMode();
            if ((_local2 is GMPlay)){
                if (isNeedingRoad){
                    _local1 = false;
                } else {
                    _local1 = m_isHighlightable;
                };
            } else {
                _local1 = true;
            };
            return (_local1);
        }
        override protected function updateArrow():void{
            if (isNeedingRoad){
                removeStagePickEffect();
                return;
            };
            switch (m_state){
                case STATE_GROWN:
                    this.createStagePickEffect();
                    break;
                case STATE_PLANTED:
                    removeStagePickEffect();
                    break;
                default:
                    this.createStagePickEffect();
            };
        }
        override protected function createStagePickEffect():void{
            var _local1:String = StagePickEffect.PICK_FORKLIFT;
            if (m_state == STATE_GROWN){
                _local1 = StagePickEffect.PICK_PREMIUM_GOODS;
            };
            if (!m_stagePickEffect){
                m_stagePickEffect = (MapResourceEffectFactory.createEffect(this, EffectType.STAGE_PICK) as StagePickEffect);
                m_stagePickEffect.setPickType(_local1);
                m_stagePickEffect.queuedFloat();
            } else {
                m_stagePickEffect.setPickType(_local1);
                m_stagePickEffect.reattach();
                m_stagePickEffect.queuedFloat();
            };
        }
        override public function onUpdate(_arg1:Number):void{
            super.onUpdate(_arg1);
            if (this.m_updateUI != null){
                this.m_updateUI();
            };
        }
        override public function updateRoadState():void{
            super.updateRoadState();
            this.updateEffects();
            this.updateArrow();
        }
        private function updateEffects():void{
            if (((!(isNeedingRoad)) && (!(isUxLockedByQuests)))){
                this.createStagePickEffect();
            } else {
                removeAnimatedEffects();
                if (m_arrow){
                    m_arrow = null;
                };
                removeStagePickEffect();
            };
        }
        override protected function getFallowToolTipStatus():String{
            return (null);
        }
        override protected function getPlowedToolTipStatus():String{
            return (null);
        }
        override protected function getGrownToolTipStatus():String{
            return (null);
        }
        override protected function getWitheredToolTipStatus():String{
            return (null);
        }
        override public function getToolTipAction():String{
            var _local1:String = super.getToolTipAction();
            if (isNeedingRoad){
                _local1 = ZLoc.t("Dialogs", "NotConnectedToRoad");
            };
            var _local2:Boolean = ((Global.isVisiting()) && (!(this.isVisitorInteractable())));
            if (_local2){
                _local1 = null;
            };
            return (_local1);
        }
        override public function onPlayAction():void{
            if (((isNeedingRoad) && (!(Global.isVisiting())))){
                super.enterMoveMode();
                return;
            };
            super.onPlayAction();
        }
        override public function onObjectDrop(_arg1:Vector3):void{
            Global.world.citySim.roadManager.updateResource(this);
            if (!isNeedingRoad){
                this.displayStatus(ZLoc.t("Main", "ConnectedToRoad"), "", 0xAA00);
            };
        }
        override protected function getTimeLeftString(_arg1:String):String{
            return (ZLoc.t("Main", "PlantedFactoryPlot", {time:_arg1}));
        }
        override protected function getPlowedToolTipAction():String{
            var _local1:String = super.getPlowedToolTipAction();
            var _local2:Boolean = ((Global.isVisiting()) && (!(this.isVisitorInteractable())));
            if (((isNeedingRoad) || (_local2))){
                return (null);
            };
            if (_local1){
                _local1 = ZLoc.t("Main", "PlowedFactoryAction");
            };
            return (_local1);
        }
        override protected function getGrownToolTipAction():String{
            var _local1:Boolean = ((Global.isVisiting()) && (!(this.isVisitorInteractable())));
            if (((isNeedingRoad) || (_local1))){
                return (null);
            };
            return (ZLoc.t("Main", "GrownFactoryAction"));
        }
        override protected function adjustModesAfterPlanting():void{
            if ((Global.world.getTopGameMode() is GMPlant)){
                Global.world.addGameMode(new GMPlay());
            };
        }
        public function getWorkerData():Array{
            var _local5:Object;
            var _local6:String;
            var _local7:Player;
            var _local1:Array = new Array();
            var _local2:Vector.<WorkerDefinition> = this.harvestingDefinition.workers.members;
            var _local3:Vector.<String> = Global.factoryWorkerManager.addAndGetWorkers(getWorkerBucket()).getWorkerIds();
            var _local4:int;
            while (_local4 < _local2.length) {
                _local5 = {};
                if (((_local3) && ((_local3.length > _local4)))){
                    _local6 = this.normalizeUid(_local3[_local4]);
                    _local7 = Global.player.findFriendById(_local6);
                    if (_local7){
                        _local5.friendName = _local7.name;
                        _local5.picUrl = _local7.snUser.picture;
                    };
                };
                _local5.position = (_local2[_local4] as WorkerDefinition).name;
                _local1.push(_local5);
                _local4++;
            };
            return (_local1);
        }
        private function normalizeUid(_arg1:String):String{
            var _local3:Number;
            var _local2:String = _arg1;
            if (_arg1){
                _local3 = parseFloat(_arg1);
                if (_local3 < 0){
                    _local3 = -1;
                };
                _local2 = _local3.toString();
            };
            return (_local2);
        }
        override public function sell():void{
            var _local1 = "";
            switch (m_state){
                case STATE_GROWN:
                    _local1 = ZLoc.t("Main", "SendToInventoryFactoryGrownWarning", {
                        item:getItemFriendlyName(),
                        coins:getSellPrice()
                    });
                    UI.displayMessage(_local1, GenericPopup.TYPE_YESNO, sellConfirmationHandler);
                    break;
                case STATE_PLANTED:
                    _local1 = ZLoc.t("Main", "SendToInventoryFactoryPlantedWarning", {
                        item:getItemFriendlyName(),
                        coins:getSellPrice()
                    });
                    UI.displayMessage(_local1, GenericPopup.TYPE_YESNO, sellConfirmationHandler);
                    break;
                default:
                    super.sell();
            };
        }
        override public function warnForStorage():Boolean{
            if (m_state == STATE_PLOWED){
                return (false);
            };
            return (true);
        }
        override public function prepareForStorage():void{
            m_state = STATE_PLOWED;
            super.prepareForStorage();
        }

    }
}//package Classes 
